Completed
Pull Request — master (#99)
by Johan
01:15
created

EAN2.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 3
b 0
f 0
1
// Encoding documentation:
2
// https://en.wikipedia.org/wiki/EAN_2#Encoding
3
4
import EANencoder from './ean_encoder.js';
5
6
class EAN2{
7
	constructor(string){
8
		this.string = string;
9
10
		this.structure = ["LL", "LG", "GL", "GG"];
11
	}
12
13
	valid(){
14
		return this.string.search(/^[0-9]{2}$/) !== -1;
15
	}
16
17
	encode(){
18
		var encoder = new EANencoder();
19
20
		// Choose the structure based on the number mod 4
21
		var structure = this.structure[parseInt(this.string) % 4];
22
23
		// Start bits
24
		var result = "1011";
25
26
		// Encode the two digits with 01 in between
27
		result += encoder.encode(this.string, structure, "01");
28
29
		return {
30
			data: result,
31
			text: this.string
32
		};
33
	}
34
}
35
36
export default EAN2;
37